<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\cms\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Cache;
use think\Db;
use app\cms\model\Attr as AttrModel;

/**
 * 属性
 */
class Attr extends Admin
{
    public function index(){
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        $order = $this->getOrder('sort ASC,attrid DESC');
        $map   = $this->getMap();
        $data_list = AttrModel::getAll($map,$order);       
        return ZBuilder::make('table')
        ->addOrder('sort,attrid')
        ->addColumns([ // 批量添加列
            ['sort', '排序'],
            // ['attrid', 'ID'],
            ['attrname', '名称'],
            ['code', '唯一代码'],
            ['inittype', '录入方式'],
            ['status', '状态' , 'status'],
            ['right_button', '操作', 'btn']
        ])
        ->setTableName('cms_attr')
        ->setPrimaryKey('attrid')
        ->addRightButton('edit')
        ->addRightButton('delete')
        ->addTopButton('add')
        ->setRowList($data_list)
        ->fetch();
    }


    /**
    *新增
    */
    public function add(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $res = $this->validate($data, 'Attr');
            if(true !== $res) return $this->error($res);

            if($data['inittype'] == 3 || $data['inittype'] == 4 || $data['inittype'] == 5){
                $data['typedetail'] = trim($data['typedetail']);
                if(empty($data['typedetail'])){
                    $this->error('请填写可选值');
                }
            }else{
                $data['type'] = 0;
            }

            $result = AttrModel::insert($data);
            if($result) {
                $this->success('新增成功');
            }else{
                $this->error('新增失败');
            }
        }

        $init_list  = ['1' => '文本', '2' => '图片', '3' => '下拉', '4' => '单选', '5' => '多选'];
        return ZBuilder::make('form')
        ->addFormItems([ // 批量添加列            
            ['text', 'attrname', '名称'],
            ['text', 'code', '唯一代码'],
            ['text', 'sort', '排序'],            
            ['select', 'inittype', '录入方式', '', $init_list],
            ['radio', 'status' , '状态', '', ['0' => '禁用' ,'1' => '启用'], '1'],
            ['textarea', 'typedetail', '可选值','<span style="color:red">多个间用英文逗号隔开</span>'],
        ])
        ->setTrigger('inittype', '3,4,5', 'typedetail,type')
        ->layout(['attrname' => 6, 'code' => 6,'sort' => 6, 'status' => 6, 'inittype' => 6])
        ->fetch();
    }

    /**
    *修改
    */
    public function edit($id = 0){
        if ($id === 0) return $this->error('缺少参数');

        if ($this->request->isPost()) {
            $data = $this->request->post();
            $res = $this->validate($data, 'Attr');
            if(true !== $res) return $this->error($res);

            if($data['inittype'] == 3 || $data['inittype'] == 4 || $data['inittype'] == 5){
                $data['typedetail'] = trim($data['typedetail']);
                if(empty($data['typedetail'])){
                    $this->error('请填写可选值');
                }
            }else{
                $data['type'] = 0;
            }
            $result = AttrModel::update($data);
            if($result) {
                $this->success('修改成功', cookie('__forward__'));
            }else{
                $this->error('修改失败');
            }
        }

        //详情
        $info      = AttrModel::where('attrid',$id)->find();
        $init_list = ['1' => '文本', '2' => '图片', '3' => '下拉', '4' => '单选', '5' => '多选'];

        return ZBuilder::make('form')
        ->addFormItems([ // 批量添加列
            ['hidden', 'attrid'],            
            ['text', 'attrname', '名称'],
            ['text','code', '唯一代码'],
            ['text', 'sort', '排序'],            
            ['select', 'inittype', '录入方式', '', $init_list],
            ['radio', 'status' , '状态', '', ['0' => '禁用' ,'1' => '启用'], '1'],
            ['textarea', 'typedetail', '可选值','<span style="color:red">多个间用英文逗号隔开</span>'],
            
        ])
        ->setTrigger('inittype', '3,4,5', 'typedetail,type')
        ->setFormData($info)
        ->layout(['attrname' => 6,'code' => 6, 'attrgroupid' => 6, 'sort' => 6, 'status' => 6, 'inittype' => 6])
        ->fetch();
    }


    /**
    *删除
    */
    public function delete($ids = 0){
        $id   = input('ids');
        $result = AttrModel::where(array('attrid' => $id))->delete();
        if($result){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

}
